package org.xgeometrywar;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class Device extends Canvas {
	public final static int COLOR_BLACK = 0;
	public final static int COLOR_WHITE = 0xffffff;
	public final static int COLOR_RED = 0xff0000;
	public final static int COLOR_GREEN = 0xff00;
	public final static int COLOR_BLUE = 0xff;
	
	public final static int KEY_NONE = 0;
	public final static int KEY_LEFT = 1 << 0;
	public final static int KEY_RIGHT = 1 << 1;
	public final static int KEY_UP = 1 << 2;
	public final static int KEY_DOWN = 1 << 3;
	public final static int KEY_POUND = 1 << 4;
	public final static int KEY_STAR = 1 << 5;
	public final static int KEY_0 = 1 << 6;
	public final static int KEY_1 = 1 << 7;
	public final static int KEY_2 = 1 << 8;
	public final static int KEY_3 = 1 << 9;
	public final static int KEY_4 = 1 << 10;
	public final static int KEY_5 = 1 << 11;
	public final static int KEY_6 = 1 << 12;
	public final static int KEY_7 = 1 << 13;
	public final static int KEY_8 = 1 << 14;
	public final static int KEY_9 = 1 << 15;
	public final static int KEY_FIRE = 1 << 16;

	public final static int KEY_LEFT_SOFT = -6;
	public final static int KEY_RIGHT_SOFT = -7;

	protected int fullScreenWidth, fullScreenHeight, halfFullScreenWidth,
			halfFullScreenHeight;

	protected int lastKeyPressed;
	protected long keyPressedTime;
	protected long keyHeldTime;
	protected int keyPressed;
	
	protected int repeatedKey = -1;
	protected void keyPressed(int keyCode) {
		keyPressedTime = System.currentTimeMillis();
		int abstractKeyCode = getAbstractKeyCode(keyCode);
		lastKeyPressed |= abstractKeyCode;
		keyPressed |= abstractKeyCode;
	}

	protected void keyReleased(int keyCode) {
		int abstractKeyCode = getAbstractKeyCode(keyCode);
		keyPressed &= ~abstractKeyCode;
		
		if(repeatedKey != 0){
			if(repeatedKey == abstractKeyCode){
				keyPressedTime = 0;
				keyHeldTime = 0;
				repeatedKey = -1;				
			}
		}
	}

	protected boolean wasKeyPressed(int abstractKey) {
		return (lastKeyPressed & abstractKey) != 0;
	}

	protected boolean isKeyPressed(int abstractKey) {
		return (keyPressed & abstractKey) != 0;
	}

	protected void paint(Graphics g) {
		
	}

	protected int getAbstractKeyCode(int keyCode) {
		switch (keyCode) {
		case Canvas.KEY_NUM0:
			return KEY_0;
		case Canvas.KEY_NUM2:
			return KEY_UP;
		case Canvas.KEY_NUM4:
			return KEY_LEFT;
		case Canvas.KEY_NUM6:
			return KEY_RIGHT;
		case Canvas.KEY_NUM8:
			return KEY_DOWN;
		case Canvas.KEY_NUM1:
			return KEY_1;
		case Canvas.KEY_NUM3:
			return KEY_3;
		case Canvas.KEY_NUM7:
			return KEY_7;
		case Canvas.KEY_NUM9:
			return KEY_9;
		case Canvas.KEY_NUM5:
			return KEY_FIRE;
		case Canvas.KEY_POUND:
			return KEY_POUND;
		case Canvas.KEY_STAR:
			return KEY_STAR;
		}
		
		try {
			int actionCode = getGameAction(keyCode);
			switch (actionCode) {
			case Canvas.LEFT:
				return KEY_LEFT;
			case Canvas.RIGHT:
				return KEY_RIGHT;
			case Canvas.UP:
				return KEY_UP;
			case Canvas.DOWN:
				return KEY_DOWN;
			}
		} catch (Exception e) {
			System.out.println("exception happens when game game action");
		}
		
		return KEY_NONE;
	}

}

